package br.ufpb.esa.plataforma.controller;

import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.sun.glass.ui.Cursor;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Point2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ListViewBuilder;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextArea;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.ImageView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class GerenciadorController implements Initializable{
	
	private Stage stage;
	private Atividade atividade;
	private int cursor = 0;
	private double orgSceneX;
	private double orgSceneY;
	@FXML
	private BorderPane menu;
	@FXML
	private SplitPane split;
	@FXML
	private AnchorPane paneDescricao;
	@FXML
	private AnchorPane paneFigura;
	@FXML
	private TextFlow textflow;
	@FXML
	private ImageView figuraInicio;
	@FXML
	private ImageView figuraFim;
	@FXML
	private Label labelAtividade;
	private List<ImageView> figurasDrag = new LinkedList<ImageView>();


	@Override
	public void initialize(URL location, ResourceBundle resources) {
		this.setLabel("Nova Atividade");
		
	}
	
	public void carregarImagens(){
		
	}
	
	public void setLabel(String novaLabel){
		this.labelAtividade.setText(novaLabel);
		this.figurasDrag.add(this.figuraInicio);
		this.figurasDrag.add(this.figuraFim);
		/*
		 * Arrasta todos os elementos da lista de imagens de uma vez
		 * consertar isso
		*/
	}
	
	@FXML
    void onDragDetected(MouseEvent evento) {
		for(ImageView imagem: this.figurasDrag){
			
			imagem.setOpacity(2);
			imagem.setMouseTransparent(true);
			imagem.setVisible(true);
			
			Dragboard dragboard = imagem.startDragAndDrop(TransferMode.ANY);
			ClipboardContent content = new ClipboardContent();
		
			content.putImage(imagem.getImage());
			dragboard.setContent(content);
			
		}
		evento.consume();	
	}	
		
		/*
		imagem.setOnMouseEntered(new EventHandler<MouseEvent>(){
			public void handle(MouseEvent event) {
				imagem.setCursor(Cursor.CURSOR_CLOSED_HAND);
				cursor = (int) event.getSceneX();
			}
			
		});
		*/
	
	
	@FXML
	void onDragOver(DragEvent event){
		for(ImageView imagem: this.figurasDrag){
			event.acceptTransferModes(TransferMode.ANY);
			DropShadow dropShadow = new DropShadow();
			dropShadow.setRadius(5.0);
			dropShadow.setOffsetX(3.0);
			dropShadow.setOffsetY(3.0);
			dropShadow.setColor(Color.color(0.4, 0.5, 0.5));
			imagem.setEffect(dropShadow);
		}
		event.consume();
	}
	
	@FXML
	void onDragDropped(DragEvent event){
		for(ImageView imagem: this.figurasDrag){
			imagem.getOnMouseClicked();
			Dragboard dragboard = event.getDragboard();
			if(dragboard.hasImage()){
				imagem.setImage(dragboard.getImage());
				Point2D localPoint = this.textflow.sceneToLocal(new Point2D(event.getSceneX(), event.getSceneY()));
				this.textflow.getChildren().remove(imagem);

				imagem.setX((int)(localPoint.getX() - imagem.getBoundsInLocal().getWidth()));
				imagem.setY((int)(localPoint.getY() - imagem.getBoundsInLocal().getHeight()));
				

				this.textflow.getChildren().add(imagem);
				event.setDropCompleted(true);
			}else{
				event.setDropCompleted(false);
			}
		}
			event.consume();
	}
	
	@FXML
	void onDragEntered(DragEvent event){
		if(event.getGestureSource() != this.textflow && event.getDragboard().hasImage()){
			this.textflow.setOpacity(2);
		}
	}
	
	@FXML
	void onDragDone(DragEvent event){
		Dragboard dragboard = event.getDragboard();
		dragboard.clear();
		event.consume();
	}
	
	@FXML
	void onDragExited(DragEvent event){
		for(ImageView imagem: this.figurasDrag){
			imagem.setEffect(null);
		}
		event.consume();
	}
	
	
}
